
* Preserve
preserve

* Marginal effects variables
gen rank = _n in 1/10
replace rank = _n - 10 in 11/20
label define dv 1 "{bf:Propensity to vote for candidate}" 2 "{bf:Vote intention for candidate}", replace
gen dv = 1 in 1/10
replace dv = 2 in 11/20
label values dv dv
forvalues i = 1/6 {
gen pe_m`i' = .
gen se_m`i' = .	
}

* Generate marginal effects including 95% CIs
foreach i of numlist 1 3 {
estimates restore m`i'
margins, dydx(treated) at(rank_cand = (1(1)10)) post
replace pe_m`i' = _b[1.treated:1bn._at] if rank == 1 & dv == 1
replace pe_m`i' = _b[1.treated:2._at] if rank == 2 & dv == 1
replace pe_m`i' = _b[1.treated:3._at] if rank == 3 & dv == 1
replace pe_m`i' = _b[1.treated:4._at] if rank == 4 & dv == 1
replace pe_m`i' = _b[1.treated:5._at] if rank == 5 & dv == 1
replace pe_m`i' = _b[1.treated:6._at] if rank == 6 & dv == 1
replace pe_m`i' = _b[1.treated:7._at] if rank == 7 & dv == 1
replace pe_m`i' = _b[1.treated:8._at] if rank == 8 & dv == 1
replace pe_m`i' = _b[1.treated:9._at] if rank == 9 & dv == 1
replace pe_m`i' = _b[1.treated:10._at] if rank == 10 & dv == 1
replace se_m`i' = _se[1.treated:1bn._at] if rank == 1 & dv == 1
replace se_m`i' = _se[1.treated:2._at] if rank == 2 & dv == 1
replace se_m`i' = _se[1.treated:3._at] if rank == 3 & dv == 1
replace se_m`i' = _se[1.treated:4._at] if rank == 4 & dv == 1
replace se_m`i' = _se[1.treated:5._at] if rank == 5 & dv == 1
replace se_m`i' = _se[1.treated:6._at] if rank == 6 & dv == 1
replace se_m`i' = _se[1.treated:7._at] if rank == 7 & dv == 1
replace se_m`i' = _se[1.treated:8._at] if rank == 8 & dv == 1
replace se_m`i' = _se[1.treated:9._at] if rank == 9 & dv == 1
replace se_m`i' = _se[1.treated:10._at] if rank == 10 & dv == 1
gen low_m`i' = pe_m`i' - 1.96 * se_m`i'
gen high_m`i' = pe_m`i' + 1.96 * se_m`i'
}
foreach i of numlist 2 {
estimates restore m`i'
margins, dydx(treated) at(rank_cand = (1(1)9)) post
replace pe_m`i' = _b[1.treated:1bn._at] if rank == 1 & dv == 1
replace pe_m`i' = _b[1.treated:2._at] if rank == 2 & dv == 1
replace pe_m`i' = _b[1.treated:3._at] if rank == 3 & dv == 1
replace pe_m`i' = _b[1.treated:4._at] if rank == 4 & dv == 1
replace pe_m`i' = _b[1.treated:5._at] if rank == 5 & dv == 1
replace pe_m`i' = _b[1.treated:6._at] if rank == 6 & dv == 1
replace pe_m`i' = _b[1.treated:7._at] if rank == 7 & dv == 1
replace pe_m`i' = _b[1.treated:8._at] if rank == 8 & dv == 1
replace pe_m`i' = _b[1.treated:9._at] if rank == 9 & dv == 1
replace se_m`i' = _se[1.treated:1bn._at] if rank == 1 & dv == 1
replace se_m`i' = _se[1.treated:2._at] if rank == 2 & dv == 1
replace se_m`i' = _se[1.treated:3._at] if rank == 3 & dv == 1
replace se_m`i' = _se[1.treated:4._at] if rank == 4 & dv == 1
replace se_m`i' = _se[1.treated:5._at] if rank == 5 & dv == 1
replace se_m`i' = _se[1.treated:6._at] if rank == 6 & dv == 1
replace se_m`i' = _se[1.treated:7._at] if rank == 7 & dv == 1
replace se_m`i' = _se[1.treated:8._at] if rank == 8 & dv == 1
replace se_m`i' = _se[1.treated:9._at] if rank == 9 & dv == 1
gen low_m`i' = pe_m`i' - 1.96 * se_m`i'
gen high_m`i' = pe_m`i' + 1.96 * se_m`i'
}
foreach i of numlist 4 6 {
estimates restore m`i'
margins, dydx(treated) at(rank_cand = (1(1)10)) post
replace pe_m`i' = _b[1.treated:1bn._at] if rank == 1 & dv == 2
replace pe_m`i' = _b[1.treated:2._at] if rank == 2 & dv == 2
replace pe_m`i' = _b[1.treated:3._at] if rank == 3 & dv == 2
replace pe_m`i' = _b[1.treated:4._at] if rank == 4 & dv == 2
replace pe_m`i' = _b[1.treated:5._at] if rank == 5 & dv == 2
replace pe_m`i' = _b[1.treated:6._at] if rank == 6 & dv == 2
replace pe_m`i' = _b[1.treated:7._at] if rank == 7 & dv == 2
replace pe_m`i' = _b[1.treated:8._at] if rank == 8 & dv == 2
replace pe_m`i' = _b[1.treated:9._at] if rank == 9 & dv == 2
replace pe_m`i' = _b[1.treated:10._at] if rank == 10 & dv == 2
replace se_m`i' = _se[1.treated:1bn._at] if rank == 1 & dv == 2
replace se_m`i' = _se[1.treated:2._at] if rank == 2 & dv == 2
replace se_m`i' = _se[1.treated:3._at] if rank == 3 & dv == 2
replace se_m`i' = _se[1.treated:4._at] if rank == 4 & dv == 2
replace se_m`i' = _se[1.treated:5._at] if rank == 5 & dv == 2
replace se_m`i' = _se[1.treated:6._at] if rank == 6 & dv == 2
replace se_m`i' = _se[1.treated:7._at] if rank == 7 & dv == 2
replace se_m`i' = _se[1.treated:8._at] if rank == 8 & dv == 2
replace se_m`i' = _se[1.treated:9._at] if rank == 9 & dv == 2
replace se_m`i' = _se[1.treated:10._at] if rank == 10 & dv == 2
gen low_m`i' = pe_m`i' - 1.96 * se_m`i'
gen high_m`i' = pe_m`i' + 1.96 * se_m`i'
}
foreach i of numlist 5 {
estimates restore m`i'
margins, dydx(treated) at(rank_cand = (1(1)9)) post
replace pe_m`i' = _b[1.treated:1bn._at] if rank == 1 & dv == 2
replace pe_m`i' = _b[1.treated:2._at] if rank == 2 & dv == 2
replace pe_m`i' = _b[1.treated:3._at] if rank == 3 & dv == 2
replace pe_m`i' = _b[1.treated:4._at] if rank == 4 & dv == 2
replace pe_m`i' = _b[1.treated:5._at] if rank == 5 & dv == 2
replace pe_m`i' = _b[1.treated:6._at] if rank == 6 & dv == 2
replace pe_m`i' = _b[1.treated:7._at] if rank == 7 & dv == 2
replace pe_m`i' = _b[1.treated:8._at] if rank == 8 & dv == 2
replace pe_m`i' = _b[1.treated:9._at] if rank == 9 & dv == 2
replace se_m`i' = _se[1.treated:1bn._at] if rank == 1 & dv == 2
replace se_m`i' = _se[1.treated:2._at] if rank == 2 & dv == 2
replace se_m`i' = _se[1.treated:3._at] if rank == 3 & dv == 2
replace se_m`i' = _se[1.treated:4._at] if rank == 4 & dv == 2
replace se_m`i' = _se[1.treated:5._at] if rank == 5 & dv == 2
replace se_m`i' = _se[1.treated:6._at] if rank == 6 & dv == 2
replace se_m`i' = _se[1.treated:7._at] if rank == 7 & dv == 2
replace se_m`i' = _se[1.treated:8._at] if rank == 8 & dv == 2
replace se_m`i' = _se[1.treated:9._at] if rank == 9 & dv == 2
gen low_m`i' = pe_m`i' - 1.96 * se_m`i'
gen high_m`i' = pe_m`i' + 1.96 * se_m`i'
}
* Graph
gen rank_m1 = rank + 0.15
gen rank_m2 = rank
gen rank_m3 = rank - 0.15
gen rank_m4 = rank + 0.15
gen rank_m5 = rank
gen rank_m6 = rank - 0.15

twoway 	(scatter pe_m1 rank_m1, mcolor(midblue) msize(small) msymbol(circle)) ///
		(rspike low_m1 high_m1 rank_m1, lcolor(midblue) lwidth(medthin)) ///
		(scatter pe_m2 rank_m2, mcolor(red) msize(small) msymbol(circle)) ///
		(rspike low_m2 high_m2 rank_m2, lcolor(red) lwidth(medthin)) ///
		(scatter pe_m3 rank_m3, mcolor(midgreen) msize(small) msymbol(circle)) ///
		(rspike low_m3 high_m3 rank_m3, lcolor(midgreen) lwidth(medthin)) ///	
		(scatter pe_m4 rank_m4, mcolor(midblue) msize(small) msymbol(circle)) ///
		(rspike low_m4 high_m4 rank_m4, lcolor(midblue) lwidth(medthin)) ///		
		(scatter pe_m5 rank_m5, mcolor(red) msize(small) msymbol(circle)) ///
		(rspike low_m5 high_m5 rank_m5, lcolor(red) lwidth(medthin)) ///
		(scatter pe_m6 rank_m6, mcolor(midgreen) msize(small) msymbol(circle)) ///
		(rspike low_m6 high_m6 rank_m6, lcolor(midgreen) lwidth(medthin)) ///	
		(connected pe_m1 rank_m1 if rank == 1, mcolor(midblue) msize(small) msymbol(circle) ///
			lcolor(midblue) lwidth(thin)) ///
		(connected pe_m2 rank_m2 if rank == 1, mcolor(red) msize(small) msymbol(circle) ///
			lcolor(red) lwidth(thin)) ///
		(connected pe_m3 rank_m3 if rank == 1, mcolor(midgreen) msize(small) msymbol(circle) ///
			lcolor(midgreen) lwidth(thin)) ///
			, ///
			ytitle("Effect of candidate-level" "versus party-level VAA advice", margin(zero) size(14pt))  ///
			yline(0, lwidth(thin) lpattern(solid) lcolor(black) extend) ///
			ylabel(, angle(horizontal) ///
				grid glcolor(gs10) glpattern(dot) glwidth(thin) gmin gmax)  ///
			xtitle("Candidate-level VAA advice (ranked)", margin(zero) size(14pt)) ///
			xscale(reverse range(0.6 10.4) noline) yscale(noline) ///
			xlabel(1 "1{superscript:st}" 2 "2{superscript:nd}" ///
				3 "3{superscript:rd}" 4 "4{superscript:th}" ///
				5 "5{superscript:th}" 6 "6{superscript:th}" ///
				7 "7{superscript:th}" 8 "8{superscript:th}" ///
				9 "9{superscript:th}" 10 "10{superscript:th}") ///
			legend(order(13 "All candidates" ///
				14 "Participating candidates" ///
				15 "Non-participating candidates") rows(1) size(14pt) ///
				span region(lcolor(none)) bmargin(0 0 0 3) symxsize(*.5) symysize(*.5)) ///
			by(dv, imargin(medium) rows(1) noiyaxes noixaxes yrescale note("") ///
				legend(on position(6))) ///
				subtitle(, size(medlarge) color(black) margin(0 0 2 0) ///
				box fcolor(white) lcolor(none) lwidth(none))  ///
			by(, scale(*1.) graphregion(fcolor(white) lcolor(white) lwidth(thin)) bgcolor(white) ///
			plotregion(fcolor(white) lcolor(white))) ///
			graphregion(fcolor(white) lcolor(white) lwidth(thin)) bgcolor(white) ///
			plotregion(fcolor(white) lcolor(black) lwidth(thin)) ///					
			scheme(s2mono) xsize(10) ysize(6) aspect(1)
* Edit y-axis
gr_edit .plotregion1.yaxis1[1].reset_rule -1 1 0.5 , tickset(major) ruletype(range) 
			

* Restore
restore